<?php
namespace App\Http\Controllers\Client;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;

class UsersController extends Controller
{
  //显示注册页
  public function showRegister()
  {
    return view('register');
  }

//注册
  public function register(Request $request)
  {
    if($request->isMethod('post'))
    {
        $rules = ['captcha' => 'required|captcha'];
        $validator = Validator::make($_POST, $rules);
        if($request->is_agree_xieyi == 11)
        {
          if($validator->fails())
          {
            session()->flash('error','验证码错误');
            return redirect('/users/register');
          }else{
            $this->validate($request, [
              'ident' => 'required|unique:users|max:50',
              'nickname'=>'required|max:8',
              'password' => 'required|min:6|max:19',
            ]);
            $user = User::create([
              'ident' => $request->ident,
              'nickname'=>$request->nickname,
              'password' => bcrypt($request->password),
            ]);
              if($user){
                session()->flash('success','亲爱的coser,恭喜注册成功');
                session(['ident' => $request->ident]);
                return redirect('/');
              }
          }
        }else{
          session()->flash('error','你未同意本社区服务协议,请阅读同意后注册');
          return redirect('/users/register');
        }
    }
  }

//登录
  public function diyLogin(Request $request)
  {
    if(Auth::guard('web')->attempt(['ident'=>$request->ident,'password'=>$request->password]))
    {
      $request->session()->put('ident', "$request->ident");
      echo json_encode(array('status'=>1,'msg'=>'登录成功'));
    }else{
      echo json_encode(array('status'=>2,'msg'=>'登录失败'));
    }
  }

//登出
  public function clientLogout(Request $request)
  {
    $request->session()->forget('ident');
    $request->session()->forget('password_hash');
    Auth::logout();
    return redirect('/');
  }

  //显示个人中心
  public function usersCenter()
  {
        $ident = session('ident');
        $nickname = DB::table('users')
                                  ->select('nickname')
                                  ->where('ident','=',$ident)
                                  ->get();
        $avatar = DB::table('users')->select('avatar')->where('ident','=',$ident)->get();
        $user_id = Auth::user()->id;
        // dd($user_id);
        // $user_id = DB::table('users')->where('ident','=',$ident)->value('id');
        //个人信息
        $info = DB::table('users')
                        ->join('article','users.id','=','article.user_id')
                        ->select('article.*','users.*')
                        ->where('users.id','=',$user_id)
                        ->orderBy('article.id', 'desc')
                        ->get();
        //我的评论数
          $commentNum = DB::table('reply')
                          ->where('user_id','=',$user_id)
                          ->count();
        //我的发帖数
          $fatieNum = DB::table('article')
                          ->where('user_id','=',$user_id)
                          ->count();
        //谁赞了我 or 谁评论了我 三表联查
          $zanData = DB::table('iszan')
                                  ->join('article','iszan.article_id','=','article.id')
                                  ->join('users','iszan.user_id','=','users.id')
                                  ->select('iszan.*','iszan.user_id as whozan','article.user_id as author_id','article.*','users.nickname','iszan.create_time as zantime')
                                  ->where('article.user_id','=',$user_id)
                                  ->orderBy('iszan.id','desc')
                                  ->paginate(11);
                              foreach($zanData as $k=>$v){
                                $dt = Carbon::parse("$v->create_time");
                                $tarr['day']=$dt->day;
                                $tarr['month']=$dt->month;
                                $tarr['time']=$dt->hour.':'.$dt->minute.':'.$dt->second;
                                $tarr['date']=$v->create_time;
                                $v->create_time=$tarr;}

        //进入个人中心后把消息标记为已读
          DB::table('iszan')
              ->join('article','article.id','=','iszan.article_id')
              ->where('article.user_id',$user_id)
              ->where('is_read','=','0')
              ->update(['is_read'=>'1']);
          DB::table('reply')
          ->join('article','article.id','=','reply.article_id')
          ->where('article.user_id',$user_id)->where('is_read','=','0')
          ->update([
            'is_read'=>'1'
          ]);
    return view('center.personal',compact('ident','nickname','info','commentNum','fatieNum','zanData','avatar'));
  }

//评的论
  public function usersComment(){
            $ident = session('ident');
            $nickname = DB::table('users')
                                      ->select('nickname')
                                      ->where('ident','=',$ident)
                                      ->get();
        $avatar = DB::table('users')->select('avatar')->where('ident','=',$ident)->get();
            // $user_id = Auth::user()->id;
            $user_id = DB::table('users')->where('ident','=',$ident)->value('id');
            $info = DB::table('users')
                            ->join('article','users.id','=','article.user_id')
                            ->select('article.*','users.*')
                            ->where('users.id','=',$user_id)
                            ->orderBy('article.id', 'desc')
                            ->paginate(11);
            $cominfo = DB::table('reply')
                                    ->join('article','article.id','=','reply.article_id')
                                    ->join('users','users.id','=','article.user_id')
                                    ->where('reply.user_id','=',$user_id)
                                    ->paginate(11);
                                    foreach($cominfo as $k=>$v){
                                      $dt = Carbon::parse("$v->create_time");
                                      $tarr['day']=$dt->day;
                                      $tarr['month']=$dt->month;
                                      $tarr['time']=$dt->hour.':'.$dt->minute.':'.$dt->second;
                                      $v->create_time=$tarr;
                                    }
        return view('center.comment',compact('ident','nickname','info','cominfo','avatar'));
  }

//发的帖
  public function usersPosted(){
            $ident = session('ident');
            $nickname = DB::table('users')
                                      ->select('nickname')
                                      ->where('ident','=',$ident)
                                      ->get();
        $avatar = DB::table('users')->select('avatar')->where('ident','=',$ident)->get();
            $user_id = Auth::user()->id;
            $info = DB::table('users')
                            ->join('article','users.id','=','article.user_id')
                            ->select('article.*','users.*','article.id as aid')
                            ->where('users.id','=',$user_id)
                            ->orderBy('article.id', 'desc')
                            ->paginate(11);
                      foreach($info as $k=>$v){
                        $dt = Carbon::parse("$v->create_time");
                        $tarr['day']=$dt->day;
                        $tarr['month']=$dt->month;
                        $tarr['time']=$dt->hour.':'.$dt->minute.':'.$dt->second;
                        $v->create_time=$tarr;
                      }
        return view('center.posted',compact('ident','nickname','info','avatar'));
  }

//填写性别
  public function insertSex(Request $request){
    $ident = session('ident');
    if($request->sex=='男'){
      $sex="1";
    }
    if($request->sex=="女"){
      $sex="3";
    }
    DB::table('users')->where('ident','=',$ident)->update(['sex'=>"1"]);
    return redirect('usersCenter');
  }
  //填写年龄
  public function insertAge(Request $request){
    $ident = session('ident');
    DB::table('users')->where('ident','=',$ident)->update(['age'=>$request->age]);
    return redirect('usersCenter');
  }

  //AJAX上传个人头像
  public function ajaxtouxiang(Request $request){
    $ident = session('ident');
    $user_id = Auth::user()->id;
    if($_FILES['avatar']['error']==0){
        if($_FILES['avatar']['size']<200000){
            $mime = array('1','image/jpg','image/png','image/bmp','image/jpeg');
            if(in_array($_FILES['avatar']['type'],$mime)){
              $old_path = DB::table('users')->where('id',$user_id)->value('avatar');
              Storage::disk('public')->delete($old_path);
              $path  = $request->file('avatar')->store('public/avatar');
              $path = strchr($path,'/');
              $z = DB::table('users')
                    ->where('id',$user_id)
                    ->update(['avatar'=>$path]);
                    if($z){
                      $arr = array('msg'=>'上传成功','status'=>'0');
                    }else{
                      $arr = array('msg'=>'上传错误','status'=>'1');
                    }
            }else{
              $arr = array('msg'=>'图片只能是JPG,PNG,BMP格式','status'=>'1');
            }
        }else{
          $arr = array('msg'=>'图片大小不能超过200KB','status'=>'1');
        }
    }else{
      $arr = array('msg'=>'图片上传失败，请联系管理员','status'=>'1');
    }
    echo json_encode($arr);
  }

//文件删除测试
  public function dddd(){
    $user_id = Auth::user()->id;
      $old_path = DB::table('users')->where('id',$user_id)->value('avatar');
      $z=Storage::disk('public')->delete('avatar/111.txt');
      dump($z);
  }
}
